import { addUrlParamsBySourceType, formatEvents, getMatchUrlEvents } from '@/mixin/url-params/url-params-util.js';

export default {
  methods: {
    addUrlParams(_dataSourceType = '') {
      const { events } = this.config;
      if (!events) {
        return;
      }
      let matchEvents = getMatchUrlEvents(events);
      matchEvents = this.clearUrlDfEvents(matchEvents);

      let { data = {} } = this.config.config;
      if (this.curComp) { // 适配左xx右xx组件data格式
        data = data[this.curComp];
      }

      let dataSourceType = _dataSourceType || data.dataSourceType;
      if (data.tooltip) { // 悬浮文本
        dataSourceType = 'xMiner';
      }
      if (['xMiner', 'xMinerPublicDatabase', 'fabric', 'Dac'].includes(dataSourceType)) {
        this.params.params = addUrlParamsBySourceType(dataSourceType, this.params.params, matchEvents);
      } else if (dataSourceType === 'xTag') {
        addUrlParamsBySourceType(dataSourceType, this.params, matchEvents);
      } else {}

      const fEvents = formatEvents(matchEvents);
      fEvents.forEach(n => {
        this.addReceivedEvents(n, 'urlEvt');
      });
    },

    // 剔除默认值事件，这些事件不参与api查询
    clearUrlDfEvents(matchEvents) {
      const dfValEvents = this.getUrlDfValEvents(matchEvents);
      const _matchEvents = {};
      Object.entries(matchEvents).map(([key, val]) => {
        if (!dfValEvents[key]) {
          _matchEvents[key] = val;
        }
      });
      return _matchEvents;
    },

    // 获取默认值事件数组，暂时只用于类型筛选
    getUrlDfValEvents(matchEvents = null) {
      const { defaultValFrom, hasDefaultVal, defaultValEvent } = this.config.config.data || {};
      if (!hasDefaultVal || defaultValFrom !== 'event' || !defaultValEvent?.length) {
        return {};
      }
      const events = {};
      let _matchEvents = matchEvents;
      if (!_matchEvents) {
        _matchEvents = getMatchUrlEvents(this.config.events);
      }

      Object.entries(_matchEvents).map(([key, val]) => {
        if (defaultValEvent.includes(key)) {
          events[key] = val;
        }
      });

      return events;
    },

  },
};
